﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

namespace CLEX.BuildDatabaseScript
{
    class Program
    {
        static void Main(string[] args)
        {
            string path = Path.Combine(new DirectoryInfo(Directory.GetCurrentDirectory()).Parent.Parent.Parent.FullName, "CLEX.Server.Database");
            string storedProcedures = Path.Combine(path, "StoredProcedures");
            string tables = Path.Combine(path, "Tables");
            string permissions = Path.Combine(path, "permissions.sql");
            StringBuilder output = new StringBuilder();
            output.AppendLine("Create Database clex");
            output.AppendLine("GO");

            output.AppendLine("Use [clex]");

            output.AppendLine("GO");

            foreach (FileInfo file in new DirectoryInfo(storedProcedures).GetFiles())
            {
                output.AppendLine(File.ReadAllText(file.FullName));
                output.AppendLine("GO");
            }
            foreach (FileInfo file in new DirectoryInfo(tables).GetFiles())
            {
                output.AppendLine(File.ReadAllText(file.FullName));
                output.AppendLine("GO");
            }
            output.AppendLine(File.ReadAllText(permissions));
            foreach (string sp in new DirectoryInfo(storedProcedures).GetFiles().Select(p => Path.GetFileNameWithoutExtension(p.Name)))
            {
                output.AppendLine(string.Format("grant execute on dbo.{0} to clex_server_dal", sp));
                output.AppendLine("GO");
            }

            //This is the has for the password "password."
            output.AppendLine("insert into clex.dbo.[user] values ( 'admin', 'e6b6afbd6d76bb5d2041542d7d2e3fac5bb05593', '1234', 'some_fake_email_for_the_admin@aol.com', 1 )");

            if (File.Exists("output.sql"))
            {
                File.Delete("output.sql");
            }
            File.WriteAllText("output.sql", output.ToString());
        }
    }
}
